<template>
  <div class="stock-in-maintenance-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>入库单维护</span>
          <el-button type="primary" @click="handleCreate">新建入库单</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="入库单号">
          <el-input v-model="queryForm.stockInNo" placeholder="请输入入库单号" />
        </el-form-item>
        <el-form-item label="入库类型">
          <el-select v-model="queryForm.type" placeholder="请选择入库类型">
            <el-option label="采购入库" value="purchase" />
            <el-option label="生产入库" value="production" />
            <el-option label="退货入库" value="return" />
            <el-option label="其他入库" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="入库日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="待审核" value="pending" />
            <el-option label="已审核" value="approved" />
            <el-option label="已入库" value="completed" />
            <el-option label="已取消" value="cancelled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="expand">
          <template #default="props">
            <el-table :data="props.row.items" border>
              <el-table-column prop="materialCode" label="物料编码" />
              <el-table-column prop="materialName" label="物料名称" />
              <el-table-column prop="specification" label="规格型号" />
              <el-table-column prop="unit" label="单位" />
              <el-table-column prop="quantity" label="数量" />
              <el-table-column prop="warehouse" label="入库仓库" />
              <el-table-column prop="batchNo" label="批次号" />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="stockInNo" label="入库单号" width="180" />
        <el-table-column prop="type" label="入库类型" width="120">
          <template #default="scope">
            {{ getTypeText(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column prop="sourceNo" label="来源单号" width="180" />
        <el-table-column prop="stockInDate" label="入库日期" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button 
              link 
              type="primary" 
              @click="handleApprove(scope.row)"
              v-if="scope.row.status === 'pending'"
            >审核</el-button>
            <el-button 
              link 
              type="success" 
              @click="handleStockIn(scope.row)"
              v-if="scope.row.status === 'approved'"
            >入库</el-button>
            <el-button 
              link 
              type="danger" 
              @click="handleCancel(scope.row)"
              v-if="['pending', 'approved'].includes(scope.row.status)"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新建/编辑入库单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'create' ? '新建入库单' : '入库单详情'"
      width="80%"
    >
      <el-form :model="stockInForm" label-width="100px" :rules="rules" ref="stockInFormRef">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="入库类型" prop="type">
              <el-select v-model="stockInForm.type" placeholder="请选择入库类型">
                <el-option label="采购入库" value="purchase" />
                <el-option label="生产入库" value="production" />
                <el-option label="退货入库" value="return" />
                <el-option label="其他入库" value="other" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="来源单号" prop="sourceNo">
              <el-input v-model="stockInForm.sourceNo" placeholder="请输入来源单号">
                <template #append>
                  <el-button @click="handleSearchSource">查询</el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库日期" prop="stockInDate">
              <el-date-picker
                v-model="stockInForm.stockInDate"
                type="date"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-table :data="stockInForm.items" border>
          <el-table-column type="index" width="50" />
          <el-table-column prop="materialCode" label="物料编码" width="150" />
          <el-table-column prop="materialName" label="物料名称" width="200" />
          <el-table-column prop="specification" label="规格型号" width="150" />
          <el-table-column prop="unit" label="单位" width="100" />
          <el-table-column prop="quantity" label="数量" width="120">
            <template #default="scope">
              <el-input-number 
                v-model="scope.row.quantity" 
                :min="1"
                controls-position="right"
              />
            </template>
          </el-table-column>
          <el-table-column prop="warehouse" label="入库仓库" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.warehouse" placeholder="请选择仓库">
                <el-option
                  v-for="item in warehouseOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="batchNo" label="批次号" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.batchNo" placeholder="请输入批次号" />
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100" fixed="right">
            <template #default="scope">
              <el-button 
                link 
                type="danger" 
                @click="handleRemoveItem(scope.$index)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="table-operations">
          <el-button type="primary" @click="handleAddItem">添加物料</el-button>
        </div>

        <el-form-item label="备注">
          <el-input v-model="stockInForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 入库操作对话框 -->
    <el-dialog
      v-model="stockInDialogVisible"
      title="入库操作"
      width="80%"
    >
      <!-- 入库操作表单 -->
      <el-form :model="stockInOperationForm" label-width="100px">
        <el-table :data="stockInOperationForm.items" border>
          <el-table-column prop="materialCode" label="物料编码" />
          <el-table-column prop="materialName" label="物料名称" />
          <el-table-column prop="specification" label="规格型号" />
          <el-table-column prop="planQuantity" label="计划数量" />
          <el-table-column label="实际入库数量" width="150">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.actualQuantity"
                :min="0"
                :max="scope.row.planQuantity"
              />
            </template>
          </el-table-column>
          <el-table-column prop="warehouse" label="入库仓库" />
          <el-table-column prop="location" label="库位" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.location" placeholder="请选择库位">
                <el-option
                  v-for="item in locationOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注" width="200">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="stockInDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleStockInSubmit">确认入库</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// ... 省略状态和方法定义 ...

// 表单验证规则
const rules = {
  type: [{ required: true, message: '请选择入库类型', trigger: 'change' }],
  sourceNo: [{ required: true, message: '请输入来源单号', trigger: 'blur' }],
  stockInDate: [{ required: true, message: '请选择入库日期', trigger: 'change' }]
}

// 入库操作提交
const handleStockInSubmit = () => {
  // 验证数量
  const valid = stockInOperationForm.items.every(item => {
    if (item.actualQuantity <= 0) {
      ElMessage.warning(`${item.materialName} 的入库数量必须大于0`)
      return false
    }
    if (item.actualQuantity > item.planQuantity) {
      ElMessage.warning(`${item.materialName} 的入库数量不能大于计划数量`)
      return false
    }
    if (!item.location) {
      ElMessage.warning(`${item.materialName} 请选择库位`)
      return false
    }
    return true
  })

  if (!valid) return

  // 实现入库逻辑
  ElMessage.success('入库成功')
  stockInDialogVisible.value = false
}
</script>

<style scoped>
.stock-in-maintenance-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.table-operations {
  margin: 20px 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  text-align: right;
}
</style> 